package com.ohos.connent.bizscenecloud.client.modular.device.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import com.google.common.collect.Lists;
import com.ohos.connent.bizscenecloud.biz.modular.device.entity.BusinessDevLibFile;
import com.ohos.connent.bizscenecloud.biz.modular.device.entity.BizMaxVersionPart;
import com.ohos.connent.bizscenecloud.biz.modular.device.enums.BizDeviceFileTypeEnum;
import com.ohos.connent.bizscenecloud.biz.modular.device.mapper.BizDeviceMapper;
import com.ohos.connent.bizscenecloud.client.modular.device.param.ClientDevicesLibraryParam;
import com.ohos.connent.bizscenecloud.client.modular.device.param.ClientScenesLibraryParam;
import com.ohos.connent.bizscenecloud.client.modular.device.result.ClientDevicesLibraryResult;
import com.ohos.connent.bizscenecloud.client.modular.device.result.ClientScenesLibraryResult;
import com.ohos.connent.bizscenecloud.client.modular.device.service.ClientDeviceService;
import com.ohos.connent.bizscenecloud.common.exception.CommonException;
import com.ohos.connent.bizscenecloud.dev.api.DevFileApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;

/**
 * @Author: ChenJiaHao
 * @Date: 2024/5/23 17:32
 * @Description:
 */
@Service
public class ClientDeviceServiceImpl implements ClientDeviceService {

    private static final String REG_EXP_PATTERN_DEVICE_LIBRARY_VERSION = "^\\d+(\\.\\d+){3}$";

    @Autowired
    BizDeviceMapper bizDeviceMapper;

    @Resource
    DevFileApi deviceFileApi;

    @Override
    public ClientDevicesLibraryResult devlibVersionManager(ClientDevicesLibraryParam clientDevicesLibraryParam) {
        BusinessDevLibFile businessDevLibFile = matchBusinessDevLibFile(clientDevicesLibraryParam.getDevlibVersion(), BizDeviceFileTypeEnum.DEVICE.getValue());
        if (ObjectUtil.isEmpty(businessDevLibFile)) {
            throw new CommonException(1001, "无更新");
        }
        String url = getUrl(businessDevLibFile);
        return new ClientDevicesLibraryResult()
                .setDevlibVersion(businessDevLibFile.getLibVersion())
                .setDescription(businessDevLibFile.getDescription())
                .setFileSize(NumberUtil.parseInt(businessDevLibFile.getSizeB()))
                .setUrl(url);
    }

    @Override
    public ClientScenesLibraryResult scenesLibVersionManager(ClientScenesLibraryParam clientScenesLibraryParam) {
        BusinessDevLibFile businessDevLibFile = matchBusinessDevLibFile(clientScenesLibraryParam.getScenelibVersion(), BizDeviceFileTypeEnum.SCENES.getValue());
        if (ObjectUtil.isEmpty(businessDevLibFile)) {
            throw new CommonException(1001, "无更新");
        }
        String url = getUrl(businessDevLibFile);
        return new ClientScenesLibraryResult()
                .setScenelibVersion(businessDevLibFile.getLibVersion())
                .setDescription(businessDevLibFile.getDescription())
                .setFileSize(NumberUtil.parseInt(businessDevLibFile.getSizeB()))
                .setUrl(url);
    }

    private BusinessDevLibFile matchBusinessDevLibFile(String libVersion, String fileType) {
        // 判断版本号是否符合格式要求，当前只支持形如1.1.1.1的数据类型
        if (!Pattern.matches(REG_EXP_PATTERN_DEVICE_LIBRARY_VERSION, libVersion)) {
            return null;
        }
        // 获取最大版本号
        BizMaxVersionPart bizMaxVersionPart = bizDeviceMapper.getMaxVersion(fileType);
        if (BeanUtil.isEmpty(bizMaxVersionPart)) {
            return null;
        }
        List<Integer> maxVersionParts = Lists.newArrayList(bizMaxVersionPart.getPart1(), bizMaxVersionPart.getPart2(), bizMaxVersionPart.getPart3(), bizMaxVersionPart.getPart4());
        List<String> inputVersionParts = Arrays.asList(libVersion.split("\\."));
        if (maxVersionParts.size() != inputVersionParts.size()) {
            return null;
        }
        BusinessDevLibFile businessDevLibFile = BeanUtil.toBean(bizMaxVersionPart, BusinessDevLibFile.class);
        for (int i = 0; i < inputVersionParts.size(); i++) {
            int devicePart = NumberUtil.parseInt(inputVersionParts.get(i));
            Integer maxPart = maxVersionParts.get(i);
            if (devicePart < maxPart) {
                return businessDevLibFile;
            }
        }
        return null;
    }

    private String getUrl(BusinessDevLibFile businessDevLibFile) {
        String url = deviceFileApi.getFileDownloadAuthUrlHUAWEI(businessDevLibFile.getFileId(), 3600L);
        if (ObjectUtil.isEmpty(url) || "没有相关文件".equals(url)) {
            throw new CommonException(1001, "无更新");
        }
        return url;
    }
}
